/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmJenisBarang.java
 *
 * Created on Jan 24, 2012, 1:28:40 PM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import usu.widget.Form;

/**
 *
 * @author yunniie
 */
public class frmJenisBarang extends Form {
private DefaultTableModel model;
    /** Creates new form frmJenisBarang */
    public frmJenisBarang() {
        initComponents();
        model=new DefaultTableModel();
        tblJenisBarang.setModel(model);
        model.addColumn("Kode Jenis Barang");
        model.addColumn("Jenis Barang");
        loadDataJenisBarang();
        
        txtKodeJenisBarang.setEnabled(false);
        txtJenisBarang.setEnabled(false);
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/icon.png")).getImage());
        setTitle(".:: Form Jenis Barang::.");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label1 = new SIMRS.Apotek.widget.Label();
        label3 = new SIMRS.Apotek.widget.Label();
        txtKodeJenisBarang = new SIMRS.Apotek.widget.TextBox();
        txtJenisBarang = new SIMRS.Apotek.widget.TextBox();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblJenisBarang = new SIMRS.Apotek.widget.Table();
        label5 = new SIMRS.Apotek.widget.Label();
        btnSimpan = new SIMRS.Apotek.widget.button2();
        btnUbah = new SIMRS.Apotek.widget.button2();
        btnHapus = new SIMRS.Apotek.widget.button2();
        btnTutup = new SIMRS.Apotek.widget.button2();

        setTitle("Form Jenis Barang");
        setAnimationHide(usu.widget.constan.Animation.HIDE_TO_LEFT);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_LEFT);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label1.setText("Kode Jenis Barang");
        pnlForm1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 110, 30));

        label3.setText("Jenis Barang");
        pnlForm1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, 100, 20));
        pnlForm1.add(txtKodeJenisBarang, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 110, 90, -1));
        pnlForm1.add(txtJenisBarang, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 140, 170, -1));

        scrollPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                scrollPane1MouseClicked(evt);
            }
        });

        tblJenisBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblJenisBarang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblJenisBarangMouseClicked(evt);
            }
        });
        scrollPane1.setViewportView(tblJenisBarang);

        pnlForm1.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 460, 170));

        label5.setForeground(new java.awt.Color(255, 255, 255));
        label5.setText("Jenis Barang ");
        label5.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 400, -1));

        btnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_simpan.png"))); // NOI18N
        btnSimpan.setText("Tambah");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        pnlForm1.add(btnSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 360, -1, -1));

        btnUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_edit.png"))); // NOI18N
        btnUbah.setText("Ubah");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });
        pnlForm1.add(btnUbah, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 360, -1, -1));

        btnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_hapus.png"))); // NOI18N
        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        pnlForm1.add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 360, -1, -1));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 360, -1, -1));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 410));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-499)/2, (screenSize.height-403)/2, 499, 403);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
        dispose();
}//GEN-LAST:event_btnTutupActionPerformed

private void scrollPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scrollPane1MouseClicked
// TODO add your handling code here:
    
        
}//GEN-LAST:event_scrollPane1MouseClicked

private void tblJenisBarangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblJenisBarangMouseClicked
// TODO add your handling code here:
    int i = tblJenisBarang.getSelectedRow();
        
        
        txtKodeJenisBarang.setText(model.getValueAt(i, 0).toString());
        txtJenisBarang.setText(model.getValueAt(i, 1).toString());
        
}//GEN-LAST:event_tblJenisBarangMouseClicked

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
// TODO add your handling code here:
    if(btnUbah.getLabel().equals("Ubah")){
            
        txtKodeJenisBarang.setEnabled(true);
        txtJenisBarang.setEnabled(true);
        
            btnUbah.setLabel("Simpan");
            tblJenisBarangMouseClicked(null);
        } else if(btnUbah.getLabel().equals("Simpan")){
 

    Connection c= KoneksiDatabase.getKoneksi(); //coding ubah
    String sql = "UPDATE jenisbarang SET JenisBarang = ? WHERE KdJenisBarang = '"+txtKodeJenisBarang.getText()+"'";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            
            statement.setString(1, txtJenisBarang.getText());
            statement.executeUpdate();
             //langsung tampil di tabel datanya
            JOptionPane.showMessageDialog(this, "Data Berhasil Diubah");
            btnUbah.setLabel("Ubah");
            txtKodeJenisBarang.setEnabled(false);
            txtJenisBarang.setEnabled(false);
            txtKodeJenisBarang.setText("");
            txtJenisBarang.setText("");
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal diubah");
          //  ex.printStackTrace();
        } finally {
                btnUbah.setLabel("Ubah");
                loadDataJenisBarang();
                tblJenisBarangMouseClicked(null);
            

        }
        }
}//GEN-LAST:event_btnUbahActionPerformed

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
// TODO add your handling code here:
    try {
        
            Connection c = KoneksiDatabase.getKoneksi();
            String sql = "DELETE FROM jenisbarang WHERE KdJenisBarang = '"+txtKodeJenisBarang.getText()+"' ";
            PreparedStatement p = c.prepareStatement(sql);
            
            p.executeUpdate();
            p.close();
            JOptionPane.showMessageDialog(this, "Data Berhasil di hapus!!!");
            txtKodeJenisBarang.setText("");
            txtJenisBarang.setText("");
    } catch (Exception e) {
        System.out.println("Terjadi error pada saat dihapus");
        } finally {
            loadDataJenisBarang();
        
    }
}//GEN-LAST:event_btnHapusActionPerformed

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
// TODO add your handling code here:
    if(btnSimpan.getLabel().equals("Tambah")){
            
            btnSimpan.setLabel("Simpan");
            txtKodeJenisBarang.setEnabled(true);
            txtJenisBarang.setEnabled(true);
            tblJenisBarangMouseClicked(null);
        } else if(btnSimpan.getLabel().equals("Simpan")){

    Connection c= KoneksiDatabase.getKoneksi(); //coding simpan
    String sql = "INSERT INTO jenisbarang (KdJenisBarang, JenisBarang) VALUES ( ?, ?)";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            statement.setString(1, txtKodeJenisBarang.getText()); //sesuai tipe data di database
            statement.setString(2, txtJenisBarang.getText());
            
            statement.executeUpdate();
            loadDataJenisBarang();  //langsung tampil di tabel datanya
            
            JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan");
            btnSimpan.setLabel("Tambah");
            txtKodeJenisBarang.setEnabled(false);
            txtJenisBarang.setEnabled(false);
            txtKodeJenisBarang.setText("");
            txtJenisBarang.setText("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal disimpan");
            
        }
        }
}//GEN-LAST:event_btnSimpanActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.Apotek.widget.button2 btnHapus;
    private SIMRS.Apotek.widget.button2 btnSimpan;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private SIMRS.Apotek.widget.button2 btnUbah;
    private SIMRS.Apotek.widget.Label label1;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.Label label5;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblJenisBarang;
    private SIMRS.Apotek.widget.TextBox txtJenisBarang;
    private SIMRS.Apotek.widget.TextBox txtKodeJenisBarang;
    // End of variables declaration//GEN-END:variables

    private void loadDataJenisBarang() {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Connection c= KoneksiDatabase.getKoneksi();
        try {
            Statement s= c.createStatement();
            String sql = "SELECT * FROM jenisbarang";
            ResultSet rs= s.executeQuery(sql);
            
            while (rs.next()){
                Object[] o = new Object[2];
                o[0]=rs.getString("KdJenisBarang");
                o[1]=rs.getString("JenisBarang");
                
                model.addRow(o);
             }
            rs.close();
            s.close();
        } catch (SQLException ex) {
            Logger.getLogger(frmJenisBarang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
